﻿/*
	wp8libflac project
	© Alovchin, 2014
*/

using System;
using System.Collections.Generic;
using System.Windows.Media;

namespace wp8.MediaHelpers
{
    /// <summary>
    /// Describes basic <see cref="MediaStreamSource" /> implementation.
    /// </summary>
    public abstract class MediaStreamSourceBase : MediaStreamSource, IDisposable
    {
        protected readonly MediaStreamSample EndOfMediaStreamSample;
        protected readonly Dictionary<MediaSampleAttributeKeys, string> MediaSampleAttributes;
        protected readonly Dictionary<MediaStreamAttributeKeys, string> MediaStreamAttributes;
        protected MediaStreamDescription MediaStreamDescription;

        protected MediaStreamSourceBase()
        {
            this.MediaStreamAttributes = new Dictionary<MediaStreamAttributeKeys, string>();
            this.MediaStreamDescription = new MediaStreamDescription(MediaStreamType.Audio, this.MediaStreamAttributes);
            this.MediaSampleAttributes = new Dictionary<MediaSampleAttributeKeys, string>();

            this.EndOfMediaStreamSample = new MediaStreamSample(
                this.MediaStreamDescription,
                null, 0, 0, 0,
                this.MediaSampleAttributes);
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Stream complete event.
        /// </summary>
        public event EventHandler StreamComplete;

        protected void RaiseStreamComplete()
        {
            EventHandler handler = this.StreamComplete;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        protected override void CloseMedia()
        {
            this.MediaStreamDescription = null;
        }

        protected virtual void Dispose(bool isDisposing)
        {
        }
    }
}